package com.ruoyi.business.controller;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.*;
import com.ruoyi.business.vo.LoginVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 机构信息Controller
 * 
 * @author ruoyi
 * @date 2022-11-23
 */
@RestController
@RequestMapping("/wechat/business")
public class BusinessWechatController extends BaseController
{

    @Autowired
    private IBusinessOrgService businessOrgService;
    @Autowired
    private IBusinessTeacherService teacherService;
    @Autowired
    private IBusinessStudentService studentService;
    @Autowired
    private IBusinessActivityService businessActivityService;
    @Autowired
    private IBusinessActivityRelationRelService activityRelationRelService;

    @PostMapping("/wechatLogin")
    public AjaxResult login(@RequestBody LoginVo loginVo)
    {
        String role = loginVo.getRole();
        String userName = loginVo.getUserName();
        String password = loginVo.getPassword();
        Session session = ShiroUtils.getSession();
        if(("1").equals(role)){
            BusinessOrg businessOrg = businessOrgService.selectBusinessOrgByUserAndPwd(userName, password);
            if(businessOrg != null){
                session.setAttribute("orgId", businessOrg.getOrgId());
                return AjaxResult.success(businessOrg);
            }else{
                return AjaxResult.error("用户名/密码错误！");
            }
        }else if(("2").equals(role)){
            BusinessTeacher businessTeacher = teacherService.selectBusinessTeacherByUserAndPwd(userName, password);
            if(businessTeacher == null) {
                businessTeacher = new BusinessTeacher();
                businessTeacher.setTeacherAccount(userName);
                businessTeacher.setTeacherPhone(password);
                teacherService.insertBusinessTeacher(businessTeacher);
            }
            session.setAttribute("teacherId", businessTeacher.getTeacherId());
            return AjaxResult.success(businessTeacher);
        }else if(("3").equals(role)){
            BusinessStudent businessStudent = studentService.selectBusinessStudentByUserAndPwd(userName, password);
            if(businessStudent == null){
                businessStudent = new BusinessStudent();
                businessStudent.setStudentAccount(userName);
                businessStudent.setStudentPwd(password);
                studentService.insertBusinessStudent(businessStudent);
            }
            session.setAttribute("studentId", businessStudent.getStudentId());
            return AjaxResult.success(businessStudent);
        }
        return AjaxResult.success("0");
    }

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public AjaxResult uploadPicture(HttpServletRequest request) {
        MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
        try {
            //对应前端的upload的name参数"file"
            MultipartFile multipartFile = req.getFile("file");
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileOrgImgName = "";
            if(multipartFile != null){
                // 上传并返回新文件名称
                fileOrgImgName = FileUploadUtils.upload(filePath, multipartFile);
            }
            return AjaxResult.success(null, fileOrgImgName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @PostMapping("/activity/add")
    @ResponseBody
    public AjaxResult addActivity(@RequestBody BusinessActivity businessActivity)
    {
        businessActivityService.insertBusinessActivity(businessActivity);

        //插入机构与活动的关系
        Long orgId = (Long)ShiroUtils.getSession().getAttribute("orgId");
        BusinessActivityRelationRel bar = new BusinessActivityRelationRel();
        bar.setActivityId(businessActivity.getActivityId());
        bar.setRelationId(orgId);
        bar.setType("1");
        activityRelationRelService.insertBusinessActivityRelationRel(bar);

        return AjaxResult.success();
    }
}
